package com.weifeng.jdsignin.jd;

import com.weifeng.jdsignin.jd.cookie.JdCookie;
import com.weifeng.jdsignin.jd.head.JdPlusSignInHead;
import com.weifeng.jdsignin.jd.poolcon.PoolCon;
import com.weifeng.jdsignin.log.ConfigLog;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;


/**
 * @description：TODO
 * @USER ：weifeng
 * @date ：2022/11/14 下午 01:54
 */
public class JdChouJingdou extends ConfigLog {
    private static String parmeter = "body=%7B%22enAwardK%22%3A%22ltvTJ%2FWYFPZcuWIWHCAjR5n7bhJVXYT" +
            "%2FmCkwmnKCPx0C4Skm1AwYUNN7BI%2FwGfc9DkRTfxOG6NQ7%5Cn5n7sLawLKgSSDJUzBvN6MjxO5mYWAtAEkgyVMwbzeuXyN7hSTr1%2F5vvjdcEzB10ntps7gemaLmWy%5Cndfy9TvjCr4%2BeWYSBJu8VekvsXrqWxjlfls0oZ%2BPVjGZWZOvkh0wPKVgrAFT2Jhkp0shO9YyKd7nt%5Cnap%2F%2BvujmCIMJd8pJPZeaDuiyTmJtn9ELBAQwF%2FaKQw5D3CvYao5B7WZeROWkzupJqz97prv7smSm%5Cnb6rISu6Bgdaeg1TcI9Lrs1sp0xxSygErWPug%2BivYVOlnltOW%2BNyLmXQ7c8JE0DpTfg2MNe9Pt0YA%5CncLIYGmbP5%2BHKCzc%3D_babel%22%2C%22awardSource%22%3A%221%22%2C%22srv%22%3A%22%7B%5C%22bord%5C%22%3A%5C%220%5C%22%2C%5C%22fno%5C%22%3A%5C%220-0-2%5C%22%2C%5C%22mid%5C%22%3A%5C%2277512871%5C%22%2C%5C%22bi2%5C%22%3A%5C%222%5C%22%2C%5C%22bid%5C%22%3A%5C%220%5C%22%2C%5C%22aid%5C%22%3A%5C%2201155413%5C%22%7D%22%2C%22encryptProjectId%22%3A%223u4fVy1c75fAdDN6XRYDzAbkXz1E%22%2C%22encryptAssignmentId%22%3A%222x5WEhFsDhmf8JohWQJFYfURTh9w%22%2C%22authType%22%3A%222%22%2C%22riskParam%22%3A%7B%22platform%22%3A%223%22%2C%22orgType%22%3A%222%22%2C%22openId%22%3A%22-1%22%2C%22pageClickKey%22%3A%22Babel_WheelSurf%22%2C%22eid%22%3A%22eidI08f18122b1saFXxwV4hTTVWtq1D8vrv3dBV8cc0BkX3DXoygudf8yzTHpL%2BUjP5n4Wxq6p%2BCNNHnoTYUoC0I5UFrACagPiIxt3R%2BguA6Ewuy72xy%22%2C%22fp%22%3A%22-1%22%2C%22shshshfp%22%3A%222ee5bec0a4f6c5cd447e399abb9a8617%22%2C%22shshshfpa%22%3A%226fb50cdc-9375-7001-f4e6-1a96383bc6f9-1656481004%22%2C%22shshshfpb%22%3A%22dXH7AP0UpXFpBkzztoe6RDQ%22%2C%22childActivityUrl%22%3A%22https%253A%252F%252Fpro.m.jd.com%252Fmall%252Factive%252F2xoBJwC5D1Q3okksMUFHcJQhFq8j%252Findex.html%253Ftttparams%253DfkBI86eyJncHNfYXJlYSI6IjFfNzJfNTU2ODRfMCIsInByc3RhdGUiOiIwIiwidW5fYXJlYSI6IjFfMjkwMV81NTU1N18wIiwiZExhdCI6IiIsImdMYXQiOiI0MC4wODIzMSIsImdMbmciOiIxMTYuMzExNjkxIiwibG5nIjoiMTE2LjQ2NzExMyIsImxhdCI6IjM5Ljk1OTUwNSIsImRMbmciOiIiLCJtb2RlbCI6ImlQaG9uZTEzLDIifQ6%25253D%25253D%22%2C%22userArea%22%3A%22-1%22%2C%22client%22%3A%22%22%2C%22clientVersion%22%3A%22%22%2C%22uuid%22%3A%22%22%2C%22osVersion%22%3A%22%22%2C%22brand%22%3A%22%22%2C%22model%22%3A%22%22%2C%22networkType%22%3A%22%22%2C%22jda%22%3A%22-1%22%7D%2C%22siteClient%22%3A%22apple%22%2C%22mitemAddrId%22%3A%22%22%2C%22geo%22%3A%7B%22lng%22%3A%22116.467113%22%2C%22lat%22%3A%2239.959505%22%7D%2C%22addressId%22%3A%22%22%2C%22posLng%22%3A%22%22%2C%22posLat%22%3A%22%22%2C%22un_area%22%3A%221_2901_55557_0%22%2C%22gps_area%22%3A%221_72_55684_0%22%2C%22homeLng%22%3A%22116.311691%22%2C%22homeLat%22%3A%2240.08231%22%2C%22homeCityLng%22%3A%22%22%2C%22homeCityLat%22%3A%22%22%2C%22focus%22%3A%22%22%2C%22innerAnchor%22%3A%22%22%2C%22cv%22%3A%222.0%22%7D&screen=1170*2259&client=wh5&clientVersion=1.0.0&sid=26a45bf8f2f57ac29a6f964a786ff38w&uuid=&area=1_2901_55557_0&ext=%7B%22prstate%22%3A%220%22%7D";

    private static Logger logger = Logger.getLogger(String.valueOf(JdChouJingdou.class));
    private static String url = "https://api.m.jd.com/client.action?functionId=babelGetLottery";


    public static void jdchoujingdou(String email, String pt_pin, String pwdt_id, String pt_key) throws IOException {

        CloseableHttpClient choujingdou =
                HttpClients.custom().setConnectionManager(PoolCon.poolCon()).build();
        HttpPost httpPost = new HttpPost(url);

        //设置请求头
        HashMap<String, String> headMap = JdPlusSignInHead.headMap();
        Iterator<String> header = headMap.keySet().iterator();
        while (header.hasNext()) {
            String key = header.next();
            httpPost.setHeader(key, headMap.get(key));
        }
        httpPost.setHeader("content-type","application/x-www-form-urlencoded");
        //设置请求参数
//        List<NameValuePair> pairs = new ArrayList<>();
//        pairs.add(new BasicNameValuePair("body","{\"enAwardK\":\"ltvTJ/WYFPZcuWIWHCAjR5n7bhJVXYT/mCkwmnKCPx0C4Skm1AwYUNN7BI" +
//                "/wGfc9DkRTfxOG6NQ7\\n5n7sLawLKgSSDJUzBvN6MjxO5mYWAtAEkgyVMwbzeuXyN7hSTr1/5vvjdcEzB10ntps7gemaLmWy" +
//                "\\ndfy9TvjCr4+eWYSBJu8VekvsXrqWxjlfls0oZ+PVjGZWZOvkh0wPKVgrAFT2Jhkp0shO9YyKd7nt\\nap" +
//                "/+vujmCIMJd8pJPZeaDuiyTmJtn9ELBAQwF/aKQw5D3CvYao5B7WZeROWkzupJqz97prv7smSm" +
//                "\\nb6rISu6Bgdaeg1TcI9Lrs1sp0xxSygErWPug+ivYVOlnltOW+NyLmXQ7c8JE0DpTfg2MNe9Pt0YA\\ncLIYGmbP5+HKCzc" +
//                "=_babel\",\"awardSource\":\"1\",\"srv\":\"{\\\"bord\\\":\\\"0\\\",\\\"fno\\\":\\\"0-0-2\\\"," +
//                "\\\"mid\\\":\\\"77512871\\\",\\\"bi2\\\":\\\"2\\\",\\\"bid\\\":\\\"0\\\"," +
//                "\\\"aid\\\":\\\"01155413\\\"}\",\"encryptProjectId\":\"3u4fVy1c75fAdDN6XRYDzAbkXz1E\"," +
//                "\"encryptAssignmentId\":\"2x5WEhFsDhmf8JohWQJFYfURTh9w\",\"authType\":\"2\"," +
//                "\"riskParam\":{\"platform\":\"3\",\"orgType\":\"2\",\"openId\":\"-1\"," +
//                "\"pageClickKey\":\"Babel_WheelSurf\"," +
//                "\"eid\":\"eidI08f18122b1saFXxwV4hTTVWtq1D8vrv3dBV8cc0BkX3DXoygudf8yzTHpL+UjP5n4Wxq6p" +
//                "+CNNHnoTYUoC0I5UFrACagPiIxt3R+guA6Ewuy72xy\",\"fp\":\"-1\"," +
//                "\"shshshfp\":\"2ee5bec0a4f6c5cd447e399abb9a8617\"," +
//                "\"shshshfpa\":\"6fb50cdc-9375-7001-f4e6-1a96383bc6f9-1656481004\"," +
//                "\"shshshfpb\":\"dXH7AP0UpXFpBkzztoe6RDQ\",\"childActivityUrl\":\"https%3A%2F%2Fpro.m.jd" +
//                ".com%2Fmall%2Factive%2F2xoBJwC5D1Q3okksMUFHcJQhFq8j%2Findex" +
//                ".html%3Ftttparams" +
//                "%3DfkBI86eyJncHNfYXJlYSI6IjFfNzJfNTU2ODRfMCIsInByc3RhdGUiOiIwIiwidW5fYXJlYSI6IjFfMjkwMV81NTU1N18wIiwiZExhdCI6IiIsImdMYXQiOiI0MC4wODIzMSIsImdMbmciOiIxMTYuMzExNjkxIiwibG5nIjoiMTE2LjQ2NzExMyIsImxhdCI6IjM5Ljk1OTUwNSIsImRMbmciOiIiLCJtb2RlbCI6ImlQaG9uZTEzLDIifQ6%253D%253D\",\"userArea\":\"-1\",\"client\":\"\",\"clientVersion\":\"\",\"uuid\":\"\",\"osVersion\":\"\",\"brand\":\"\",\"model\":\"\",\"networkType\":\"\",\"jda\":\"-1\"},\"siteClient\":\"apple\",\"mitemAddrId\":\"\",\"geo\":{\"lng\":\"116.467113\",\"lat\":\"39.959505\"},\"addressId\":\"\",\"posLng\":\"\",\"posLat\":\"\",\"un_area\":\"1_2901_55557_0\",\"gps_area\":\"1_72_55684_0\",\"homeLng\":\"116.311691\",\"homeLat\":\"40.08231\",\"homeCityLng\":\"\",\"homeCityLat\":\"\",\"focus\":\"\",\"innerAnchor\":\"\",\"cv\":\"2.0\"}"));
//        pairs.add(new BasicNameValuePair("screen","1170*2259"));
//        pairs.add(new BasicNameValuePair("client","wh5"));
//        pairs.add(new BasicNameValuePair("clientVersion","1.0.0"));
//        pairs.add(new BasicNameValuePair("sid","26a45bf8f2f57ac29a6f964a786ff38w"));
//        pairs.add(new BasicNameValuePair("uuid",""));
//        pairs.add(new BasicNameValuePair("area","1_2901_55557_0"));
//        pairs.add(new BasicNameValuePair("ext","{\"prstate\":\"0\"}"));
//
//        httpPost.setEntity(new UrlEncodedFormEntity(pairs,"utf-8"));


        //设置 cookie   从excel中取出参数  添加到请求参数中


        httpPost.setEntity(new StringEntity(parmeter));

        logger.info("以下是 抽京豆 读取的内容");
        HashMap<String, String> cookieMap = JdCookie.cookieMap();
        logger.info("读取到的 email："+email);
        cookieMap.put("pt_pin",pt_pin);
        logger.info("读取到的 pt_pin："+pt_pin);
        cookieMap.put("pwdt_id",pwdt_id);
        logger.info("读取到的 getPwdt_id："+pwdt_id);
        cookieMap.put("pt_key",pt_key);
        logger.info("读取到的 pt_key："+pt_key);
        //把 map 转成string，但是 收尾 有 {}，先把 {} 去掉再放到cookie中
        String s = cookieMap.toString().replace(",",";");;
        httpPost.setHeader("cookie", s.substring(1, s.length() - 1));

        CloseableHttpResponse execute = choujingdou.execute(httpPost);
        System.out.println(EntityUtils.toString(execute.getEntity()));
    }
}
